package com.itheima_02;

public class StringBuilderDemo {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        System.out.println("拼接前：");
        getArray(arr);
        String s = arraytoString(arr);
        System.out.println("--------");
        System.out.println("拼接后：");
        System.out.println(s);
    }

    public static String arraytoString(int[] arr) {
        StringBuilder sb = new StringBuilder("[");
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                sb.append(arr[i]).append("]");
            } else {
                sb.append(arr[i]).append(",");
            }
        }
//        String s = sb.toString();
//        return s;
        return sb.toString();
    }

    public static void getArray(int[] arr) {
        System.out.print("{");
        for (int i = 0; i < arr.length; i++) {
            if (i < arr.length - 1) {
                System.out.print(arr[i] + ",");
            } else {
                System.out.println(arr[i] + "}");
            }
        }
    }
}
